﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Bill.BasDLL.biz;
using TT.ORM;
using Bill.BasDLL.ety;

namespace Bill.BasForm
{
    public partial class frmBillPrice : Dev.Control.BaseFormDev
    {
        string favSeqId;
        bizBillPrice biz;
        TT.Module.sysLogin.etyBaeSystemParamter basSysPara;
        public string ClientId;
        public string BillCode;
        public string BussType;
        public string Area;
        public string SF;
        public string City;
        public string QX;
        IList<TT.ORM.etyBaseTable> ilSF;
        IList<TT.ORM.etyBaseTable> ilCity;
        IList<TT.ORM.etyBaseTable> ilQX;
        IList<etyBillPrice> ilBillPrice;

        public frmBillPrice(TT.Module.sysLogin.etyBaeSystemParamter basSysPara, string favSeqId, IList<TT.ORM.etyCodeRef> ilBussType, IList<TT.ORM.etyBaseTable> ilSF, IList<TT.ORM.etyBaseTable> ilCity, IList<TT.ORM.etyBaseTable> ilQX)
        {
            this.favSeqId = favSeqId;
            InitializeComponent();
            this.basSysPara = basSysPara;
            biz = new bizBillPrice(basSysPara);

            //业务类型
            string sMapingCols = "CODE_DESC";
            string sMapingHeaderTxt = "业务类型";
            string sColumnWidth = "20";
            cboType.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboType.DisplayMember = "CODE_DESC";
            cboType.ValueMember = "CODE";
            cboType.DataSource = ilBussType;

            //省
            this.ilSF = ilSF;
            sMapingCols = "X_CNAME";
            sMapingHeaderTxt = "省份";
            sColumnWidth = "20";
            cboSF.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboSF.DisplayMember = "X_CNAME";
            cboSF.ValueMember = "X_CODE";
            cboSF.DataSource = ilSF;
            //市
            this.ilCity = ilCity;
            sMapingCols = "X_CNAME";
            sMapingHeaderTxt = "市";
            sColumnWidth = "20";
            cboCity.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboCity.DisplayMember = "X_CNAME";
            cboCity.ValueMember = "X_CODE";
            cboCity.DataSource = ilCity;
            //院
            this.ilQX = ilQX;
            sMapingCols = "X_CNAME";
            sMapingHeaderTxt = "院";
            sColumnWidth = "20";
            cboQX.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboQX.DisplayMember = "X_CNAME";
            cboQX.ValueMember = "X_CODE";
            cboQX.DataSource = ilQX;
        }

        public override Dev.Control.etyQuickKey F10()
        {
            AddPrice();
            return base.F10();
        }
        public override Dev.Control.etyQuickKey F11()
        {
            UpdatePrice();
            return base.F11();
        }
        public override Dev.Control.etyQuickKey F9()
        {
            DeletePrice();
            return base.F9();
        }

        private void DeletePrice()
        {
            etyBillPrice etyPrice = srcBinding.Current as etyBillPrice;
            if (etyPrice == null || string.IsNullOrWhiteSpace(etyPrice.PRICE_SEQ))
            {
                MessageBox.Show("请选择要删除的记录！");
                return;
            }
            if (MessageBox.Show("确定要删除选中的记录？", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;
            etyPrice.PORT_CODE = basSysPara.PORT_CODE;
            if (biz.Delete(etyPrice))
            {
                MessageBox.Show("删除成功！");
                GetBillPrice();
            }
            else
            {
                MessageBox.Show("删除失败！");
            }
        }

        private void AddPrice()
        {
            etyBillPrice etyPrice = new etyBillPrice();
            etyPrice.PORT_CODE = basSysPara.PORT_CODE;
            etyPrice.LST_MOD = "ADDPRICE";
            etyPrice.LST_USER = basSysPara.UserID;
            etyPrice.CUSTOMER = cboClient.EditValue.ToString();
            etyPrice.ITEM = cboBillCode.EditValue.ToString();
            etyPrice.METHOD = cboMethod.EditValue.ToString();
            etyPrice.QTY = txtQty.Text;
            etyPrice.FROM_QTY = txtFromQty.Text;
            etyPrice.TO_QTY = txtToQty.Text;
            etyPrice.QTY_PER = txtQtyPer.Text;
            etyPrice.PRICE = txtPrice.Text;
            etyPrice.DISCOUNT = txtDiscount.Text;
            etyPrice.PRICE_BAS = txtPriceBas.Text;
            etyPrice.DISCOUNT_BAS = txtDiscountBas.Text;
            etyPrice.BUSS_TYPE = cboType.EditValue.ToString();
            etyPrice.AREA = cboLoc.EditValue.ToString();
            etyPrice.SF = (string.IsNullOrWhiteSpace(lbSfCode.Text) ? cboSF.EditValue.ToString() : lbSfCode.Text);
            etyPrice.CITY = (string.IsNullOrWhiteSpace(lbCityCode.Text) ? cboCity.EditValue.ToString() : lbCityCode.Text);
            etyPrice.QX = (string.IsNullOrWhiteSpace(lbQXCode.Text) ? cboQX.EditValue.ToString() : lbQXCode.Text);
            etyPrice.PRICE_BAS = txtQtyBas.Text;
            etyPrice.FAV_SEQ_ID = favSeqId;
            etyPrice.QTY_BAS = txtQtyBas.Text;
            if (string.IsNullOrWhiteSpace(etyPrice.CUSTOMER))
            {
                MessageBox.Show("请选择客户代码！");
                return;
            }
            if (string.IsNullOrWhiteSpace(etyPrice.ITEM))
            {
                MessageBox.Show("请选择收费代码！");
                return;
            }
            if (!CheckInput(etyPrice))
                return;

            etyPrice.UNIT_PER = cboBillCode.GetColumnValue("ITEM_UNIT").ToString();
            if (biz.Insert(etyPrice))
            {
                MessageBox.Show("保存成功！");
                GetBillPrice();
            }
            else
            {
                MessageBox.Show("保存失败！");
            }
        }
        private void UpdatePrice()
        {
            etyBillPrice etyPrice = srcBinding.Current as etyBillPrice;
            if (etyPrice == null || string.IsNullOrWhiteSpace(etyPrice.PRICE_SEQ))
            {
                MessageBox.Show("请选择要编辑的记录！");
                return;
            }
            etyPrice.PORT_CODE = basSysPara.PORT_CODE;
            etyPrice.LST_MOD = "UPDPRICE";
            etyPrice.LST_USER = basSysPara.UserID;
            etyPrice.METHOD = cboMethod.EditValue.ToString();
            etyPrice.QTY = txtQty.Text;
            etyPrice.FROM_QTY = txtFromQty.Text;
            etyPrice.TO_QTY = txtToQty.Text;
            etyPrice.QTY_PER = txtQtyPer.Text;
            etyPrice.PRICE = txtPrice.Text;
            etyPrice.DISCOUNT = txtDiscount.Text;
            etyPrice.PRICE_BAS = txtPriceBas.Text;
            etyPrice.DISCOUNT_BAS = txtDiscountBas.Text;
            etyPrice.BUSS_TYPE = cboType.EditValue.ToString();
            etyPrice.AREA = cboLoc.EditValue.ToString();
            etyPrice.SF = (string.IsNullOrWhiteSpace(lbSfCode.Text) ? cboSF.EditValue.ToString() : lbSfCode.Text);
            etyPrice.CITY = (string.IsNullOrWhiteSpace(lbCityCode.Text) ? cboCity.EditValue.ToString() : lbCityCode.Text);
            etyPrice.QX = (string.IsNullOrWhiteSpace(lbQXCode.Text) ? cboQX.EditValue.ToString() : lbQXCode.Text);
            etyPrice.QTY_BAS = txtQtyBas.Text;
            if (!CheckInput(etyPrice))
                return;
            if (biz.Update(etyPrice))
            {
                MessageBox.Show("更新成功！");
                GetBillPrice();
            }
            else
            {
                MessageBox.Show("更新失败！");
            }
        }

        private bool CheckInput(etyBillPrice etyPrice)
        {
            if (string.IsNullOrWhiteSpace(etyPrice.BUSS_TYPE))
            {
                MessageBox.Show("请选择类型！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyPrice.AREA))
            {
                MessageBox.Show("请选择区域！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyPrice.SF))
            {
                MessageBox.Show("请选择省份！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyPrice.CITY))
            {
                MessageBox.Show("请选择市！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyPrice.QX))
            {
                MessageBox.Show("请选择院！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyPrice.METHOD))
            {
                MessageBox.Show("请选择模式！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyPrice.QTY) && string.IsNullOrWhiteSpace(etyPrice.FROM_QTY))
            {
                MessageBox.Show("请输入数量！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyPrice.QTY_PER))
            {
                MessageBox.Show("请输入每数量！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyPrice.PRICE))
            {
                MessageBox.Show("请输入单价！");
                return false;
            }
            return true;
        }

        private void frmBillPrice_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(favSeqId))
                toolStrip1.Visible = false;

            FillComBox();

            gdPrice.GridView = this.gridView1;
            string sMapingCols = "CUSTOMER,CLIENT_NAME,ITEM,ITEM_DESC,BUSS_TYPE,BUSS_TYPE_DESC,AREA,SF_DESC,CITY_DESC,QX_DESC,METHOD,PRICE_BAS,QTY_BAS,QTY,FROM_QTY,TO_QTY,QTY_PER,UNIT_PER,DISCOUNT_BAS,PRICE,DISCOUNT,FROM_DATE,TO_DATE,PRICE_SEQ";
            string sMapingHeaderTxt = "客户代码,客户名称,收费代码,收费描述,类型代码,类型描述,区域,省份,市,院,模式,起计资费,起计重量,数量,数量(从),数量(到),每数量,每单位,起计折扣,单价,单价折扣,开始日期,结束日期,流水号";
            gdPrice.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
            this.winUtil.LoadGridLayout(gdPrice);
            if (!string.IsNullOrWhiteSpace(ClientId))
                cboClient.EditValue = ClientId;
            if (!string.IsNullOrWhiteSpace(BillCode))
                cboBillCode.EditValue = BillCode;
            if (!string.IsNullOrWhiteSpace(BussType))
                cboType.EditValue = BussType;
            if (!string.IsNullOrWhiteSpace(Area))
                cboLoc.EditValue = Area;
            if (!string.IsNullOrWhiteSpace(SF))
                cboSF.EditValue = SF;
            if (!string.IsNullOrWhiteSpace(City))
                cboCity.EditValue = City;
            if (!string.IsNullOrWhiteSpace(QX))
                cboQX.EditValue = QX;
            GetBillPrice();

        }
        private void FillComBox()
        {
            //客户
            string sMapingCols = "CLIENT_NAME";
            string sMapingHeaderTxt = "客户名称";
            string sColumnWidth = "20";
            cboClient.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboClient.DisplayMember = "CLIENT_NAME";
            cboClient.ValueMember = "CLIENT_ID";
            cboClient.DataSource = biz.GetClient();

            //收费代码
            sMapingCols = "ITEM_DESC";
            sMapingHeaderTxt = "收费描述";
            sColumnWidth = "20";
            cboBillCode.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboBillCode.DisplayMember = "ITEM_DESC";
            cboBillCode.ValueMember = "ITEM";
            cboBillCode.DataSource = biz.GetBillCode();

            //区域
            sMapingCols = "AREA_NAME";
            sMapingHeaderTxt = "区域";
            sColumnWidth = "20";
            cboLoc.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboLoc.DisplayMember = "AREA_NAME";
            cboLoc.ValueMember = "AREA_NAME";
            cboLoc.DataSource = new List<etyArea>();

            //模式
            DataTable dtMethod = new DataTable();
            dtMethod.Columns.Add("METHOD");
            dtMethod.Columns.Add("DESCPT");
            dtMethod.Rows.Add("*", "每");
            dtMethod.Rows.Add("E", "每(向下取整)");
            dtMethod.Rows.Add("O", "每(向上取整)");
            dtMethod.Rows.Add(">", "大于...时每");
            dtMethod.Rows.Add("G", "大于...时只");
            dtMethod.Rows.Add("=", "只是");
            sMapingCols = "METHOD,DESCPT";
            sMapingHeaderTxt = "模式,描述";
            sColumnWidth = "20,80";
            cboMethod.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboMethod.DisplayMember = "DESCPT";
            cboMethod.ValueMember = "METHOD";
            cboMethod.DataSource = dtMethod;
        }

        private void FillArea()
        {
            string clientId = cboClient.EditValue.ToString();
            string bussType = cboType.EditValue.ToString();
            if (!string.IsNullOrWhiteSpace(clientId) && !string.IsNullOrWhiteSpace(bussType))
            {
                cboLoc.DataSource = biz.GetArea(clientId, bussType);
            }
        }
        private void cboClient_TextChanged(object sender, EventArgs e)
        {
            if (cboClient.Focus())
            {
                //GetBillPrice();
                IList<etyBillPrice> ilCurPrice = new List<etyBillPrice>();
                foreach (etyBillPrice eyPrice in ilBillPrice)
                {
                    if (eyPrice.CLIENT_NAME == cboClient.Text)
                    {
                        ilCurPrice.Add(eyPrice);
                    }
                }
                srcBinding.DataSource = ilCurPrice;
                FillArea();
            }
        }

        private void cboBillCode_TextChanged(object sender, EventArgs e)
        {
            GetBillPrice();
        }
        private void GetBillPrice()
        {
            string clientId = cboClient.EditValue.ToString();
            string billCode = cboBillCode.EditValue.ToString();

            ilBillPrice = biz.GetBillPrice("", clientId, billCode);
            srcBinding.DataSource = ilBillPrice;
            gdPrice.DataSource = srcBinding;

            radMthPer.Checked = false;
            radMthMore.Checked = true;
            txtQty.EditValue = "";
            txtFromQty.EditValue = "";
            txtToQty.EditValue = "";
            txtQtyPer.EditValue = "";
            txtPrice.EditValue = "";
            txtDiscount.EditValue = "";
            txtPriceBas.EditValue = "";
            txtDiscountBas.EditValue = "";
        }

        public override Dev.Control.etyQuickKey RefreshDate()
        {
            GetBillPrice();
            return base.RefreshDate();
        }

        private void gdPrice_Click(object sender, EventArgs e)
        {
            etyBillPrice etyPrice = srcBinding.Current as etyBillPrice;
            if (etyPrice == null || string.IsNullOrWhiteSpace(etyPrice.PRICE_SEQ))
            {
                return;
            }
            cboClient.TextChanged -= cboClient_TextChanged;
            cboClient.EditValue = etyPrice.CUSTOMER;
            cboClient.TextChanged += cboClient_TextChanged;
            cboBillCode.TextChanged -= cboBillCode_TextChanged;
            cboBillCode.EditValue = etyPrice.ITEM;
            cboBillCode.TextChanged += cboBillCode_TextChanged;
            cboType.EditValue = etyPrice.BUSS_TYPE;
            cboLoc.EditValue = etyPrice.AREA;
            //cboSF.EditValue = etyPrice.SF;
            //cboCity.EditValue = etyPrice.CITY;
            //cboQX.EditValue = etyPrice.QX;
            if (!etyPrice.SF.Contains(','))
            {
                cboSF.EditValue = etyPrice.SF;
                lbSfCode.Text = "";
                lbSfName.Text = "";
            }
            else
            {
                cboSF.EditValue = "";
                lbSfCode.Text = etyPrice.SF;
                lbSfName.Text = etyPrice.SF_DESC;
            }
            if (!etyPrice.CITY.Contains(','))
            {
                cboCity.EditValue = etyPrice.CITY;
                lbCityCode.Text = "";
                lbCityName.Text = "";
            }
            else
            {
                cboCity.EditValue = "";
                lbCityCode.Text = etyPrice.CITY;
                lbCityName.Text = etyPrice.CITY_DESC;
            }
            if (!etyPrice.QX.Contains(','))
            {
                cboQX.EditValue = etyPrice.QX;
                lbQXCode.Text = "";
                lbQXName.Text = "";
            }
            else
            {
                cboQX.EditValue = "";
                lbQXCode.Text = etyPrice.QX;
                lbQXName.Text = etyPrice.QX_DESC;
            }
            //cboMethod.EditValue = etyPrice.METHOD;
            foreach (Control ctrl in gpPrice.Controls)
            {
                if (ctrl is RadioButton)
                {
                    RadioButton radBtn = (RadioButton)ctrl;
                    if (radBtn.Tag.ToString() == etyPrice.METHOD)
                    {
                        radBtn.Checked = true;
                        break;
                    }
                }
            }
            txtQty.Text = etyPrice.QTY;
            txtFromQty.Text = etyPrice.FROM_QTY;
            txtToQty.Text = etyPrice.TO_QTY;
            txtQtyPer.Text = etyPrice.QTY_PER;
            txtPrice.Text = etyPrice.PRICE;
            txtDiscount.Text = etyPrice.DISCOUNT;
            txtPriceBas.Text = etyPrice.PRICE_BAS;
            txtDiscountBas.Text = etyPrice.DISCOUNT_BAS;
            txtQtyBas.Text = etyPrice.QTY_BAS;
        }

        private void cboType_TextChanged(object sender, EventArgs e)
        {
            FillArea();
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            AddPrice();
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            UpdatePrice();
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            DeletePrice();
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            F12();
        }

        private void radMethod_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton radBtn = sender as RadioButton;
            if (radBtn.Checked)
            {
                cboMethod.EditValue = radBtn.Tag.ToString();
                switch (radBtn.Tag.ToString())
                {
                    case ">":
                    case "G":
                        txtFromQty.Visible = true;
                        lbQtyFromTo.Visible = true;
                        txtToQty.Visible = true;
                        //lbQtyPer.Visible = true;
                        //txtQtyPer.Visible = true;
                        txtQty.Text = "";
                        txtQty.Visible = false;
                        break;
                    default:
                        txtFromQty.Text = "";
                        txtToQty.Text = "";
                        txtFromQty.Visible = false;
                        lbQtyFromTo.Visible = false;
                        txtToQty.Visible = false;
                        //txtQtyPer.Text = "";
                        //lbQtyPer.Visible = false;
                        //txtQtyPer.Visible = false; 
                        txtQty.Visible = true;
                        break;
                }
            }
        }

        private void btnSF_Click(object sender, EventArgs e)
        {
            frmSelCitys frm = new frmSelCitys("S", ilSF);
            frm.SelNo = lbSfCode.Text;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                cboSF.EditValue = "";
                lbSfCode.Text = frm.SelNo;
                lbSfName.Text = frm.SelName;
            }
        }

        private void btnCity_Click(object sender, EventArgs e)
        {

            frmSelCitys frm = new frmSelCitys("C", ilCity);
            frm.SelNo = lbCityCode.Text;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                cboCity.EditValue = "";
                lbCityCode.Text = frm.SelNo;
                lbCityName.Text = frm.SelName;
            }
        }

        private void btnQX_Click(object sender, EventArgs e)
        {
            frmSelCitys frm = new frmSelCitys("Y", ilQX);
            frm.SelNo = lbQXCode.Text;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                cboQX.EditValue = "";
                lbQXCode.Text = frm.SelNo;
                lbQXName.Text = frm.SelName;
            }
        }

        private void cboSF_TextChanged(object sender, EventArgs e)
        {
            lbSfCode.Text = "";
            lbSfName.Text = "";
        }

        private void cboCity_TextChanged(object sender, EventArgs e)
        {
            lbCityCode.Text = "";
            lbCityName.Text = "";
        }

        private void cboQX_TextChanged(object sender, EventArgs e)
        {
            lbQXCode.Text = "";
            lbQXName.Text = "";
        }

    }
}
